package servlets.customer;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pojo.User;
import services.ServiceCart;
import services.ServiceUser;

public class ServletSignIn extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String email = request.getParameter("email");
        String password = request.getParameter("password");

        ServiceUser serviceUser = new ServiceUser();

        String login = serviceUser.login(email, password);
        
            response.getWriter().print(login);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String email = request.getParameter("email");
        String password = request.getParameter("password");

        ServiceUser serviceUser = new ServiceUser();

        String login = serviceUser.login(email, password);
        if ("Sorry User Not active".equals(login)) {

        } else if ("Wrong User data, please try again".equals(login)) {
            
        } else {
            HttpSession session = request.getSession();
            session.setAttribute("userId", login);
            User user =serviceUser.getUserData(Integer.parseInt(login));
            Cookie cookie = new Cookie("username", user.getUsername());
            
            response.addCookie(cookie);
              Cookie cookie1 = new Cookie("id", ""+user.getId());
            
            response.addCookie(cookie1);
        }

        response.getWriter().print(login);
    }
}
